﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/* 
* Application  : Velsto
* Class        : RunGMTFile
* 01/06/2011   : M.A.van Ravesteijn (Raafcom)
*   02/09/2010   Initial version v1.0

* 
* Class for Methods around XY files (file with key XYPointEntity XY Pairs)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace Velsto
{
    public class RunGMTFile
    {
        public RunGMTFile()
        {
        }

        public string saveRunGMTFile(SimulationEntity se,string triangulateexe,
            string nearneighborexe,string grd2xyzexe, string surfaceexe,
            string GMTshr, string GMTbin)
        {
            try
            {
                TextWriter rdFile = new StreamWriter(se.RunGmtFile);
                rdFile.WriteLine("set GMT_SHAREDIR=" + GMTshr);
                rdFile.WriteLine("set PATH=$PATH;" + GMTbin);
                rdFile.WriteLine("date /t");
                rdFile.WriteLine("time /t");
                rdFile.WriteLine("copy " + "\"" + se.GriddingInFile + "\"" + " xyzin.xyz");
                switch (se.GriddingType) 
                {
                    case Constants.GRIDDINGTYPE_TRIANG :
                        rdFile.WriteLine(
                            "\"" + triangulateexe + "\"" + 
                            " xyzin.xyz " 
                            + "-ENaN "
                            + "-R" 
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                            + "="
                            + " -Gtest.grd"
                            + " 2>&1"
                            );
                        break;
                    case Constants.GRIDDINGTYPE_NEARNEIGH :
                        rdFile.WriteLine(
                            "\"" + nearneighborexe + "\"" +
                            " xyzin.xyz " 
                            + " -R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                            + "="
                            + " -Lg -Gtest.grd"
                            + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -N4/2"
                            + " 2>&1"
                        );
                        break;
                    case Constants.GRIDDINGTYPE_CONTCURV:
                        rdFile.WriteLine(
                            "\"" + surfaceexe + "\"" +
                            " xyzin.xyz "
                            + " -R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture) 
                            + "="
                            + " -Gtest.grd"
                            + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                            + " 2>&1"
                        );
                        break;
                }
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("\"" + grd2xyzexe + "\"" + " test.grd " + " -R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + (se.GridXMax-se.GridInterval).ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + (se.GridYMax-se.GridInterval).ToString("0.0", CultureInfo.InvariantCulture) + 
                            " > xyz3.xyz");
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("copy xyz3.xyz " + "\"" + se.GriddingOutFile + "\"");
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("del xyz3.xyz");
                //rdFile.WriteLine("del xyzin.xyz");
                rdFile.WriteLine("date /t");
                rdFile.WriteLine("time /t");
                rdFile.Close();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        public string gc_saveRunGMTFile(SimulationEntity se, string grdmathexe,
            string triangulateexe,string nearneighborexe,string surfaceexe, string grd2xyzexe, 
            string GMTshr, string GMTbin)
        {
            try
            {
                TextWriter rdFile = new StreamWriter(se.RunGmtFile);
                rdFile.WriteLine("set GMT_SHAREDIR=" + GMTshr);
                rdFile.WriteLine("set PATH=$PATH;" + GMTbin);
                rdFile.WriteLine("date /t");
                rdFile.WriteLine("time /t");
                rdFile.WriteLine("copy " + "\"" + se.Gc_File1 + "\"" + " xyz1.xyz");
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                switch (se.GriddingType)
                {
                    case Constants.GRIDDINGTYPE_TRIANG:
                        rdFile.WriteLine(
                            "\"" + triangulateexe + "\"" +
                            " xyz1.xyz "
                            + "-ENaN "
                            + "-R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                            + "="
                            + " -Ggrd1.grd"
                            + " 2>&1"
                            );
                        break;
                    case Constants.GRIDDINGTYPE_NEARNEIGH:
                        rdFile.WriteLine(
                            "\"" + nearneighborexe + "\"" +
                            " xyz1.xyz "
                            + " -R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                            + "="
                            + " -Lg -Ggrd1.grd"
                            + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -N4/2"
                            + " 2>&1"
                        );
                        break;
                    case Constants.GRIDDINGTYPE_CONTCURV:
                        rdFile.WriteLine(
                            "\"" + surfaceexe + "\"" +
                            " xyz1.xyz "
                            + " -R"
                            + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                            + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                            + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                            + "="
                            + " -Ggrd1.grd"
                            + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                            + " 2>&1"
                        );
                        break;
                } 
                
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                if (se.Gc_GridGrid)
                {
                    rdFile.WriteLine("copy " + "\"" + se.Gc_File2 + "\"" + " xyz2.xyz");
                    switch (se.GriddingType)
                    {
                        case Constants.GRIDDINGTYPE_TRIANG:
                            rdFile.WriteLine(
                                "\"" + triangulateexe + "\"" +
                                " xyz2.xyz "
                                + "-ENaN "
                                + "-R"
                                + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                                + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                                + "="
                                + " -Ggrd2.grd"
                                + " 2>&1"
                                );
                            break;
                        case Constants.GRIDDINGTYPE_NEARNEIGH:
                            rdFile.WriteLine(
                                "\"" + nearneighborexe + "\"" +
                                " xyz2.xyz "
                                + " -R"
                                + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                                + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                                + "="
                                + " -Lg -Ggrd2.grd"
                                + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                                + " -N4/2"
                                + " 2>&1"
                            );
                            break;
                        case Constants.GRIDDINGTYPE_CONTCURV:
                            rdFile.WriteLine(
                                "\"" + surfaceexe + "\"" +
                                " xyz2.xyz "
                                + " -R"
                                + se.GridXMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridXMax.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMin.ToString("0.0", CultureInfo.InvariantCulture) + "/"
                                + se.GridYMax.ToString("0.0", CultureInfo.InvariantCulture)
                                + " -I" + se.GridInterval.ToString("0.0", CultureInfo.InvariantCulture)
                                + "="
                                + " -Ggrd2.grd"
                                + " -S" + se.GriddingSearchRadius.ToString("0.0", CultureInfo.InvariantCulture)
                                + " 2>&1"
                            );
                            break;
                    } 
                    rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                }
                string oper = "ADD";
                switch (se.Gc_Operator) 
                {
                    case Constants.CONTCALCOPER_ADD :
                        oper = "ADD";
                        break;
                    case Constants.CONTCALCOPER_SUB :
                        oper = "SUB";
                        break;
                    case Constants.CONTCALCOPER_MULT :
                        oper = "MUL";
                        break;
                    case Constants.CONTCALCOPER_DIV :
                        oper = "DIV";
                        break;
                }
                if (se.Gc_GridGrid)
                {
                    rdFile.WriteLine(
                                  "\"" + grdmathexe + "\"" +
                                  " grd1.grd "
                                  + " grd2.grd "
                                  + oper
                                  + " = grdout.grd"
                                  + " 2>&1"
                                 );
                }
                else
                {
                    rdFile.WriteLine(
                                  "\"" + grdmathexe + "\"" +
                                  " grd1.grd "
                                  + se.Gc_Constant.ToString("0.0", CultureInfo.InvariantCulture)
                                  + " " + oper
                                  + " = grdout.grd"
                                  + " 2>&1"
                                 );
                }
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("\"" + grd2xyzexe + "\"" + " grdout.grd > " + "xyz3.xyz");
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("copy xyz3.xyz " + "\"" + se.Gc_FileOut + "\"");
                rdFile.WriteLine("IF ERRORLEVEL 1 EXIT 1");
                rdFile.WriteLine("del xyz3.xyz");
                rdFile.WriteLine("date /t");
                rdFile.WriteLine("time /t");
                rdFile.Close();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
